//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<XSimple.cpp>
///	@path	~/src/scene/xsimple/
///	@date	2007/12/26
///	@desc	.

#include "config/config.h"

#include "scene/camera/CCameraBase.h"
#include "scene/xsimple/XSimple.h"

namespace xeres {

	IMPL_IOBJECT_CLASS_DYNAMIC( XSimple );

	// ctor
	XSimple::XSimple( void )
	{
	}

	// dtor
	XSimple::~XSimple( void )
	{
	}

	// Initialize
	bool XSimple::Initialize( const WString& name , ResourceLoader * loader , RefWeak<IData> res )
	{
		if( !m_name.empty() )
		{
			TRACE_ERROR( _S("XSimple::Initialize: The scene has been initialized.") );
			return false;
		}

		m_name = name;

		Ref<IData> config = res->OpenData( name + _S(".xsimple") );

		// Read basic configurations.
		config->GetAttribute( _S("title") , m_title );

		// Read all sub entries & create them.
		std::vector< Ref<IData> > lives;
		config->OpenDataArray( _S("lives/live") , lives );

		for( size_t i = 0 , total = lives.size() ; i < total ; ++i )
		{
			RefPass<ILive> newLive = CreateLive( loader , lives[i] );

			// create new
			m_lives.push_back( newLive );
		}

		return true;
	}

	// Finalize
	void XSimple::Finalize( void )
	{
		
	}

	// Preload
	void XSimple::Preload( const Vector<float>& center )
	{

	}

	// Enter
	void XSimple::Enter( RefWeak<ICamera> camera )
	{
		if( m_camera.IsNull() )
			return;

		m_camera = camera;

		// set starting position
	}

	// Leave
	void XSimple::Leave( void )
	{
		m_camera = NULL;
	}

	// Tick
	void XSimple::Tick( const Frame& frame )
	{
		if( m_camera.IsValid() )
			m_camera->Tick( frame );
	}

	// Present
	void XSimple::Present( ScenePresentation& sp )
	{
		for( size_t i = 0 , total = m_lives.size() ; i < total ; ++i )
		{
			m_lives[i]->Present( sp );
		}
	}

	// Synchronized
	void XSimple::Synchronized( void )
	{

	}

	// GetBound
	const Box<float>& XSimple::GetBound( void )
	{
		return m_bound;
	}
	
} // namespace xeres
